#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
转码模板模型
"""

from sqlalchemy import Column, Integer, String, Text, Boolean, DateTime, JSON, Float
from sqlalchemy.sql import func
from app.core.database import Base
import json

class TranscodeTemplate(Base):
    __tablename__ = "transcode_templates"
    
    id = Column(Integer, primary_key=True, index=True)
    name = Column(String(100), nullable=False, unique=True, comment="模板名称")
    description = Column(String(500), nullable=True, comment="模板描述")
    
    # 封装格式
    container_format = Column(String(20), nullable=False, default="mp4", comment="封装格式: mp4, flv, ts, mkv")
    
    # 视频参数
    video_codec = Column(String(20), nullable=False, default="h264", comment="视频编码: h264, h265")
    video_bitrate = Column(Integer, nullable=False, default=2000, comment="视频码率(kbps)")
    video_resolution = Column(String(20), nullable=False, default="1920x1080", comment="分辨率")
    video_fps = Column(Integer, nullable=False, default=25, comment="帧率")
    video_profile = Column(String(20), nullable=True, default="high", comment="编码档次")
    video_preset = Column(String(20), nullable=True, default="medium", comment="预设")
    
    # 音频参数
    audio_codec = Column(String(20), nullable=False, default="aac", comment="音频编码: aac, mp3")
    audio_bitrate = Column(Integer, nullable=False, default=128, comment="音频码率(kbps)")
    audio_sample_rate = Column(Integer, nullable=False, default=44100, comment="采样率")
    audio_channels = Column(Integer, nullable=False, default=2, comment="声道数")
    
    # 图片水印
    image_watermark_enabled = Column(Boolean, default=False, comment="是否启用图片水印")
    image_watermark_path = Column(String(500), nullable=True, comment="水印图片路径")
    image_watermark_position = Column(String(20), nullable=True, default="top-right", comment="水印位置")
    image_watermark_opacity = Column(Integer, default=100, comment="水印透明度(0-100)")
    image_watermark_scale = Column(Float, default=1.0, comment="水印缩放比例")
    
    # 文字水印
    text_watermark_enabled = Column(Boolean, default=False, comment="是否启用文字水印")
    text_watermark_content = Column(String(200), nullable=True, comment="文字内容")
    text_watermark_position = Column(String(20), nullable=True, default="bottom-right", comment="文字位置")
    text_watermark_font = Column(String(100), nullable=True, default="Arial", comment="字体")
    text_watermark_size = Column(Integer, default=24, comment="字体大小")
    text_watermark_color = Column(String(20), nullable=True, default="#FFFFFF", comment="字体颜色")
    text_watermark_opacity = Column(Integer, default=100, comment="文字透明度(0-100)")
    
    # 高级选项
    custom_ffmpeg_args = Column(Text, nullable=True, comment="自定义FFmpeg参数")
    
    # 状态
    is_default = Column(Boolean, default=False, comment="是否为默认模板")
    is_active = Column(Boolean, default=True, comment="是否启用")
    
    # 时间戳
    created_at = Column(DateTime(timezone=True), server_default=func.now())
    updated_at = Column(DateTime(timezone=True), onupdate=func.now())
    
    def __repr__(self):
        return f"<TranscodeTemplate(id={self.id}, name={self.name})>"
    
    def to_dict(self):
        return {
            "id": self.id,
            "name": self.name,
            "description": self.description,
            "container_format": self.container_format,
            "video_codec": self.video_codec,
            "video_bitrate": self.video_bitrate,
            "video_resolution": self.video_resolution,
            "video_fps": self.video_fps,
            "video_profile": self.video_profile,
            "video_preset": self.video_preset,
            "audio_codec": self.audio_codec,
            "audio_bitrate": self.audio_bitrate,
            "audio_sample_rate": self.audio_sample_rate,
            "audio_channels": self.audio_channels,
            "image_watermark_enabled": self.image_watermark_enabled,
            "image_watermark_path": self.image_watermark_path,
            "image_watermark_position": self.image_watermark_position,
            "image_watermark_opacity": self.image_watermark_opacity,
            "image_watermark_scale": self.image_watermark_scale,
            "text_watermark_enabled": self.text_watermark_enabled,
            "text_watermark_content": self.text_watermark_content,
            "text_watermark_position": self.text_watermark_position,
            "text_watermark_font": self.text_watermark_font,
            "text_watermark_size": self.text_watermark_size,
            "text_watermark_color": self.text_watermark_color,
            "text_watermark_opacity": self.text_watermark_opacity,
            "custom_ffmpeg_args": self.custom_ffmpeg_args,
            "is_default": self.is_default,
            "is_active": self.is_active,
            "created_at": self.created_at.isoformat() if self.created_at else None,
            "updated_at": self.updated_at.isoformat() if self.updated_at else None
        }